package pl.edu.agh.pros.camera.provider;

import org.jivesoftware.smack.XMPPException;
import org.ros.RosCore;
import org.ros.node.NodeConfiguration;

import pl.edu.agh.pros.conf.ProxyConfiguration;
import pl.edu.agh.pros.connector.IServerConnector;
import pl.edu.agh.pros.connector.gae.GAEServerConnector;
import pl.edu.agh.pros.proxy.RosProxy;
import pl.edu.agh.pros.proxy.RosProxyHandler;

public class Main {

	private static final String PROVIDER_APP_KEY = "20012115a151-e968-4ec5-8063-e761cacc7217"; //$NON-NLS-1$
	private IServerConnector serverConnector = new GAEServerConnector("http://rosproxy.appspot.com/"); //$NON-NLS-1$
	private NodeConfiguration nodeConfiguration;

	private RosProxy rosProxy;
	private RosProxyHandler rosProxyHandler;
	private RosProxyHandler rosProxyHandler2;

	public void start() throws InterruptedException {
		RosCore rosCore = RosCore.newPrivate(3331);
		rosCore.start();
		rosCore.awaitStart();
		nodeConfiguration = NodeConfiguration.newPrivate();
		nodeConfiguration.setMasterUri(rosCore.getUri());

		try {
			rosProxy = RosProxy.newInstance(PROVIDER_APP_KEY, "ProviderID", nodeConfiguration, serverConnector); //$NON-NLS-1$
			rosProxy.start();
			rosProxy.awaitStart();
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		rosProxyHandler = rosProxy.createProxyForTopicSubscriber(new ProxyConfiguration("/camera/image/compressed", //$NON-NLS-1$
				sensor_msgs.CompressedImage._TYPE));
		rosProxyHandler.start();
		rosProxyHandler.awaitStart();

		rosProxyHandler2 = rosProxy.createProxyForTopicSubscriber(new ProxyConfiguration("/koala/sensors", //$NON-NLS-1$
				std_msgs.String._TYPE));
		rosProxyHandler2.start();
		rosProxyHandler2.awaitStart();

	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		Main main = new Main();
		main.start();
		System.in.read();
	}

}
